package cn.com.pajx.pajx_visitor_v3.api;

import android.text.TextUtils;

import com.google.gson.Gson;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.com.pajx.pajx_visitor_v3.BaseApp;
import cn.com.pajx.pajx_visitor_v3.bean.SchoolBean;
import cn.com.pajx.pajx_visitor_v3.utils.AESUtil;
import cn.com.pajx.pajx_visitor_v3.utils.AppConstant;
import cn.com.pajx.pajx_visitor_v3.utils.CommonUtil;
import cn.com.pajx.pajx_visitor_v3.utils.LogUtils;
import cn.com.pajx.pajx_visitor_v3.utils.MD5Util;
import cn.com.pajx.pajx_visitor_v3.utils.RSAUtil;
import cn.com.pajx.pajx_visitor_v3.utils.SharePreferencesUtil;

/**
 * create by ruancw on 2024/8/22
 * description 公共参数封装
 */
public class HttpParamsUtil {

    /**
     * 生成带有sign的map
     */
    static LinkedHashMap<String, String> addSignToMap(LinkedHashMap<String, String> map) {
        String schoolData = SharePreferencesUtil.getInstance().getString(AppConstant.SCHOOL_BEAN);
        SchoolBean schoolBean = new Gson().fromJson(schoolData, SchoolBean.class);
        if (!TextUtils.isEmpty(schoolData) && schoolBean != null) {
            map.put("scl_id", schoolBean.getScl_id());
            map.put("equ_no", schoolBean.getEqu_no());
            map.put("equ_id", schoolBean.getEqu_id());
            map.put("dev_id", schoolBean.getDev_id());
        }
        //app_id
        map.put("app_id", AppConfig.APP_ID);
        //版本号
        map.put("app_version", CommonUtil.getVersionName(BaseApp.getInstance()));
        //唯一请求标识
        map.put("nonce", CommonUtil.getRandomNonce());
        //时间戳
        map.put("timestamp", String.valueOf(System.currentTimeMillis()));
        //参数名按ASCII 码从小到大排序
        List<Map.Entry<String, String>> infoIds = new ArrayList<>(map.entrySet());
        Collections.sort(infoIds, (o1, o2) -> (o1.getKey()).compareTo(o2.getKey()));
        //参数名及参数拼接
        StringBuilder params = new StringBuilder();
        for (int n = 0; n < infoIds.size(); n++) {
            Map.Entry<String, String> entry = infoIds.get(n);
            //处理 key value
            String key = entry.getKey();
            String value = entry.getValue();

            if (TextUtils.isEmpty(value)) {
                value = "";
                map.put(key, value);
            }
            //处理[]
            if (key.contains("[") && key.contains("]")) {
                //处理
                key = key.substring(0, key.indexOf("["));
            }
            params.append(key);
            params.append("=");
            params.append(value);
        }
        String params_value = params + AppConfig.SIGN_KEY;
        map.put("sign", MD5Util.MD5(params_value));
        return map;

    }

    /**
     * 将map参数集合转换成json
     *
     * @param map map参数集合
     * @return json字符串
     */
    public static String paramsToJson(LinkedHashMap<String, String> map) {
        Gson gson = new Gson();
        return gson.toJson(addSignToMap(map));
    }

    /**
     * 生成json形式的body
     *
     * @param data data参数
     * @return 字符串
     */
    static LinkedHashMap<String, String> generateBodyJson(String data, String aes_key) {
        LogUtils.e("data=" + data.replaceAll("\\\\", ""));
        LogUtils.e("aes_key=" + aes_key);
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        try {
            String encrypt_key = RSAUtil.encryptByPublicKey(RSAUtil.loadPublicKeyByStr(AppConfig.RSA_PUBLIC), aes_key.getBytes());
            LogUtils.e("encrypt_key=" + encrypt_key);
            LogUtils.e("encrypt_key_encode=" + URLEncoder.encode(encrypt_key));
            String encrypt_data = AESUtil.encryptCBC(data, aes_key);
            LogUtils.e("encrypt_data=" + encrypt_data);
            LogUtils.e("decrypt_data=" + AESUtil.decryptCBC(encrypt_data, aes_key));
            map.put("app_id", AppConfig.APP_ID);
            map.put("encrypt_key", URLEncoder.encode(encrypt_key));
            map.put("data", URLEncoder.encode(encrypt_data));
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

}
